//
//  VNViewController.swift
//  SSCodingOsx
//
//  Created by Toj on 3/16/21.
//  Copyright © 2021 VIPCODE. All rights reserved.
//

import Cocoa
import SnapKit

class VNViewController: NSViewController {
    
    var titleBarHeight: CGFloat = VNTitleBar.DefaultTitleHeight {
        didSet {
            setupMakeLayoutSubviews()
        }
    }
    
    lazy var titleBar: VNTitleBar = {
        let view = VNTitleBar()
        view.isHidden = true
        return view
    }()
    
    deinit {
        print("VNViewController : " + #function)
    }
}

extension VNViewController {
    
    override func loadView() {
        view = NSView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupMakeAddSubviews()
    }
    override func updateViewConstraints() {
        super.updateViewConstraints()
        setupMakeLayoutSubviews()
    }
}

// MARK: - private
extension VNViewController {
    
}

// MARK: - setup
extension VNViewController {
    
    private func setupMakeAddSubviews() {
        view.addSubview(titleBar)
    }
    
    private func setupMakeLayoutSubviews() {
        titleBar.snp.remakeConstraints { (make) in
            make.left.right.top.equalTo(view)
            make.height.equalTo(titleBarHeight)
        }
    }
}

extension VNViewController: NSWindowDelegate {
    
    func windowShouldClose(_ sender: NSWindow) -> Bool {
        print("VNViewController : " + #function)
        return true
    }
    
    func windowWillClose(_ notification: Notification) {
        print("VNViewController : " + #function)
    }
    
    func windowDidResize(_ notification: Notification) {
        guard let win = notification.object as? VNWindow else { return }
        win.relayoutTitlebar()
    }
}
